
libname ra "E:\data";/*cohort*/
libname pd "Z:\Medicare_20\Part_D";
libname pb "Z:\Medicare_20\Part_b";

*****************PURPOSE:IDENTIFY DESCALATION EVENT FOR EACH  INDEX PART B MEDICATION
                    1. Flag and identify interval increase (rx int differenc between 2 fill dates in weeks)
                     confirm stable use 6m prior to possible interval increase, confirm sustained interval increase in next fill dt
                     and get corresponding date of interval increase
                    2. Flag and identify cessation and get dates
                    3. Identify the first descalation per each id*****************;



%macro partb_med (med=  , name= );

*************get dates of increase rx interval and date of 6m prior to that to assess stability******;
data &name ;
   set ra.coh_ana5a;

   where index_med1= "&med" ;/*index_med1 codes for source of goli*/

   drop yr_partd yr_b  SRVC_DT  PDE_ID Medication_name diff_d 
   med_name diff_b diff_d  RA_b_icd;


  /*FLAG OBSRVATIONS with ANY increase interval (6 months after index30 for partb) */
   if index30_lagrx_int ge 183 and diff_int_w gt 0 then inc_rx_int=1;

   if inc_rx_int=1 then incint_dt= lag_rx_dt;/*use the date of last time when interval is stable*/
	
   /* get event date minus 6 months to asses stability*/
   incint_dt_6m = intnx('DAY',incint_dt,-183);
  
   format incint_dt date9. incint_dt_6m date9. ;

  /*no event in first 6 months after index30*/

    if index30_lagrx_int lt 183 then do;

     incint_dt = . ; 
     incint_dt_6m = .;
     inc_rx_int= .; 
 
     end;

run;

**********INCREASED INTERVAL AND  STABILITY OF USE BEFROE************************
****************************************************************************;

/*PURPOSE: IDENTIFY STABILITY OF USE BEFORE INT INCREASE***/
/*SORT by descending*/
proc sort data= &name out= &name._1 ;
  by bene_id descending rx_dt descending incint_dt ; run;

/*make incint_dt and incint_dt_6m same per each int increase*/
data &name._1;
  set &name._1;

     by bene_id;
 retain incint_dt_6m2 incint_dt2 ;

 if first.bene_id then do;/*setting to . at the begining for each id*/

   incint_dt_6m2= .;
 
    incint_dt2=. ;
 
 end;

 ****use retain to keep the value of new var*****;
 if incint_dt_6m > . then incint_dt_6m2= incint_dt_6m ;/*if missing then carry backward)*/
 else incint_dt_6m = incint_dt_6m2;/*if not missing keep the prev vaue*/

 if incint_dt > . then incint_dt2= incint_dt ;/*if missing then carry backward)*/
 else incint_dt = incint_dt2;

drop   incint_dt2 incint_dt_6m2 ;

run;

/**sort back by  rx_dt to regular chronological order*/
proc sort data= &name._1 out= &name._2 ;by bene_id rx_dt ;run;

/*6m prior to each int increase , create the time window*/
data &name._2;
   set &name._2;

   by bene_id;

   rx_incint6 = rx_dt - incint_dt_6m;/* int between rx date and event dt-6m date*/

/* var to code stable interval,  set to missing at the time of event, and obs beyond 6 month dt
so stable int can be assessed from obs before event until 6 mothns before event*/

   /*binary if within 6m window*/
 if rx_incint6 ge 0 and rx_incint6 le 183 then int_6=1; else int_6=0;
 
/*create diff_int_w to be availbale in 6m prior window, outsode window its set to missing*/
 if rx_incint6 lt 0  or rx_incint6 gt 183 then diff_int_w1 = . ;/*183 is the time of event*/
 else diff_int_w1= diff_int_w;/*outside 6m window, set to . else take value of diff_int_w */

/*lag vars*/

 lag_rx_incint6= lag (rx_incint6);
 lag_int6= lag (int_6);
 lag_rx_int= lag(rx_int);

if first.bene_id then do
   lag_rx_incint6= . ;
   lag_rx_int= .;
   lag_int6= .;

 end;

 ****code diff in the interval between inc dt and inc dt -6m as missing, as
 this value lies outside of the time interval of interest*;

 if int_6 ne lag_int6 then diff_int_w1= . ; 

 /*use absolute value to calculate sum of diff */

 diff_int_w2 = abs (diff_int_w1); 
  
run;

*******************interval increase***************************************;
******************confirm stable use prior*********************;

proc sort data= &name._2 out= &name._int  ;by bene_id  incint_dt; run;

/*get sum of diff int for each potential event date
  value >0 indicates unstable use */
proc means data= &name._int  sum noprint;
    by bene_id incint_dt;
     var diff_int_w2;
     output out=sum_diff  sum=Sum_diff_int ;
run;

/*get sum of numbers of days between each potential event date 
 ( for first event it is number of days since index date
event flag are coded such that the event cannot happen in 6 months since index30 date)*/
proc means data= &name._int sum noprint;
    by bene_id incint_dt;
    var lag_rx_int;
    output out=sum_rx_int sum=Sum_rx_int ;
run;

proc sort data= sum_rx_int out= sum_1 (drop= _TYPE_ _FREQ_) ; by bene_id incint_dt;run;
proc sort data= sum_diff out= sum_2 (drop= _TYPE_ _FREQ_) ; by bene_id incint_dt;run;
proc sort data= &name._2 ; by bene_id incint_dt ; run;

************indentify increased interval event with stable prior use*****************;
data &name._3;

  merge &name._2 sum_1 sum_2;

  by bene_id incint_dt;

 ***************interval increase with confirmend stable use in 6m prior**********;

****************NON SWITCHERS*************;

  if idswitch ne 1 then do;

/* increase int AND stable diff in past 6 m*/
  if inc_rx_int=1 and sum_diff_int=0 and sum_rx_int ge 183 then event_incint=1;

  else event_incint=0;

  end;

/*******SWITCHERS, look for event during index med only**********/
if idswitch =1 then do;

  if  inc_rx_int=1 and sum_diff_int=0 and sum_rx_int ge 183 and (incint_dt < switch_dt)
  then event_incint=1;/*if increase int happened before switch*/

  else event_incint=0;/*not stable/ not during index med*/

end;

***********IF INCREASE INTERVAL QUALIFIES AS CESSATION**************;
/*gap >90, then it qualifies for cessation and not increase interval*/

  if rx_int gt 90 then event_incint = . ;
 

run;/*event date is incint_dt*/

/*sort by regular order*/

proc sort data= &name._3 ; by bene_id rx_dt; run;

**************Increase interval contd...sustained increase intrval until next visit***************;

****sort by desc to PULL the next 2 obs up using lag function***;
proc sort data= &name._3 out=sus;  by bene_id descending rx_dt ;run;

data sus;
set sus;

  next1_diff_int_w= lag( diff_int_w);
  next2_diff_int_w= lag (next1_diff_int_w);/*lag of lag*/

  if event_incint=1;
  /*keeping only obs at time of event; lag and lag of lag by descending to pull next1 and 2 obs*/

 keep bene_id rx_dt diff_int_w next1_diff_int_w next2_diff_int_w;
 run;

/*sort by regular order*/
proc sort data= sus; by bene_id rx_dt;run;
proc sort data= &name._3; by bene_id rx_dt;run;

data &name._3;
 merge &name._3 sus;

  by bene_id rx_dt;

*****************SUS INTERVAL INCREASE************************;

/*sustained until next 1 rx dt*/
  if event_incint=1 and next1_diff_int_w lt 0 then event_incint_sus1=0;
  else if event_incint=1 and next1_diff_int_w ge 0 then event_incint_sus1=1;/*sus or more increase*/

/*sustained until next 2 rx dt*/
  if event_incint=1 and event_incint_sus1=1 and  next2_diff_int_w lt 0 then event_incint_sus2=0;
  else if event_incint=1 and event_incint_sus1=1 and next2_diff_int_w ge 0 then event_incint_sus2=1;


      if event_incint_sus1 =1 then sus1_incint_dt= incint_dt;

      format sus1_incint_dt date9.;

run;

proc sort data= &name._3 ; by bene_id;run;

*********************Cessation*********************;
data  &name._3a;
    set &name._3;
    by bene_id;

 /*1. ****************FLAG GAPS > 90 FOR CESSATION*************;

    /*no event in first 7 m*/
    if index30_lagrx_int ge 183 and rx_int gt 90 then event_cess=1; 

  ************* get dates of potential cesssation *******;
 if event_cess=1 and idswitch ne 1 then do;
    cess_dt = lag_rx_dt;
    cess_index=1;/*ind for cess during index***/
	end;

 *****************switchers, but cessation happened during primn rx**********;
 if event_cess=1 and idswitch=1 and rx_dt =<switch_dt then do;
    cess_dt= lag_rx_dt;
    cess_index=1;/*ind for cess during index***/  

    end; 

 ***************switchers and cessation happened during the first switch******;
if event_cess=1 and idswitch=1 and (rx_dt >switch_dt) and
      switch_dt = lag_rx_dt then do;
    cess_dt= switch_dt;/* at the time of first switch*/
    cess_switch=1;

	end;
  *subsequent switches switch_dt < lag_rx_dt doesnt count for cessation
 (switch_dt indicates time of FIRST swtich ,NTV)*;
   if event_cess=1 and idswitch=1 and (switch_dt < lag_rx_dt) then  event_cess= . ;

   
 /*2******true stop gap >90 between last rx and end of fu/death among those who were on index med*********/

  if last.bene_id and idswitch = . then do;

  if lastrx_end gt 90 then event_cess=1;
   if lastrx_end gt 90 and event_cess=1 then cess_dt= last_rx_dt;

 end;

*******create index2 date, dt of start of FU**********;

   index2 = intnx ('DAY',index_dt,213);/*7mths for part b index med*/;

  format cess_dt date9. sus1_incint_dt date9. index2 date9. ;

 run;

**************get first increase int event for each id***********************;
**int**;
  data int;
  set &name._3a;

  if event_incint_sus1=1 and (sus1_incint_dt>index2);
    keep bene_id   sus1_incint_dt;

   rename sus1_incint_dt= incintdt_first;
   run;

proc sort data= int; by bene_id ;run;

data int1;
set int;
by bene_id;
if first.bene_id;
run;

proc sort data= int1; by bene_id ;run;
proc sort data= &name._3a; by bene_id ;run;

data &name._4;
merge &name._3a int1 ;
by bene_id ;
run;


****************** get ids with cessation ***************;

data cess;
  set &name._4;
  if event_cess=1 and (cess_dt>index2);
  keep bene_id   cess_dt;
  rename cess_dt= cess_dt_first;
run;


proc sort data= cess; by bene_id ;run;

data cess1;
set cess;
by bene_id;
if first.bene_id;
run;

proc sort data= &name._4; by bene_id ;run;

data &name._5 ;
  merge &name._4 cess1 ;
  by bene_id ;
run;

*****************IDENTIFY FIRST DE_ESCALTION*********************;

**************get first desc date******************;
data ra.&name._5a;
set &name._5;

********only increase interval******************;
if incintdt_first ne . and  cess_dt_first = . then do;
    desc_dt= incintdt_first;
    desc= "Interval increase";
end;

***********only cessation******************;
else if  incintdt_first= . and  cess_dt_first ne . then do;
    desc_dt= cess_dt_first;
   desc= "Cessation";
end;

***************both*********************;
 else if  incintdt_first ne . and  cess_dt_first ne . then do;

 *****************inc int first*****;
if  incintdt_first < cess_dt_first  then desc_dt= incintdt_first;
if  incintdt_first < cess_dt_first  then desc= "Interval increase";

**************cess first***************;
if incintdt_first > cess_dt_first then desc_dt= cess_dt_first;
if incintdt_first > cess_dt_first then desc = "Cessation";

end;

******************neither cess or inc int*******************;
if  incintdt_first = . and  cess_dt_first = . then desc= "None";

format desc_dt date9.;

run;

%mend;
%partb_med (med=Inflixima, name= infl);
%partb_med (med= Goli_b, name= goli);








